<?php
/**
 * Created by 杜鹏
 * createTime  :2015/9/23 0023下午 3:14
 * description :
 */
namespace Home\Model;


use Think\Model;

class TreememberModel extends Model
{
    protected $_validate = array(
        array('name','require','姓名不能为空!'), //默认情况下用正则进行验证
        array('tel','/^1\d{10}$/','手机号不正确!',0),
        array('add_1','require','请填写省份!'), //默认情况下用正则进行验证
        array('add_2','require','请填写城市!'), //默认情况下用正则进行验证
        array('add_3','require','请填写区/县!'), //默认情况下用正则进行验证
        array('add_4','require','请填写详细地址!') //默认情况下用正则进行验证
    );
    /**
     * 更新方法
     */
    public function update()
    {
        $data = $this->create();
        if(!$data){ //数据对象创建错误
            return false;
        }
        /* 添加或更新数据 */
        if(empty($data['id'])){
            $res = $this->add();
        }else{
            $res = $this->save();
        }
        return $res;
    }

    /**
     * 获取会员信息
     */
    public function getinfo($id)
    {
        return $this->field(true)->where('id='.$id)->find();
    }

    /**
     * 验证手机号
     */
    public function check_tel($tel)
    {
        return $this->field(true)->where('tel='.$tel)->find();
    }

    /**
     * 新增会员加分
     * @return boolean fasle 失败
     * @param 推荐人ID
     */
    public function addintegral($uid)
    {
        $arr_id = D('treestruct')->getpid_arr($uid);
        /**获取代理ID***/
        $arr_id = D('treestruct')->getagentid($uid);
        $agent_id = $agent['id'];
        M()->startTrans();
        if ($uid != 1 || $agent['lvl'] != null) {
            $agent = $this->where('uid='.$agent_id)->setInc('integral',C('integral_config_6w'));
            $data = array('uid'=>$agent_id,'type'=>'4','integral'=>'+'.C('integral_config_6w'),'changetime'=>time(),'changeuid'=>$_SESSION['onethink_admin']['user_auth']['uid']);
            $integral = M('integralchange');
            $res = $integral->data($data)->add();
            if ($res === false) {
                M()->rollback();
            }
        }
        if ($arr_id === false) {
            $first = $this->where('uid='.$uid)->setInc('integral',C('integral_config_first'));
            $data = array('uid'=>$uid,'type'=>'4','integral'=>'+'.C('integral_config_first'),'changetime'=>time(),'changeuid'=>$_SESSION['onethink_admin']['user_auth']['uid']);
            $integral = M('integralchange');
            $res = $integral->data($data)->add();
            if ($res === false) {
                M()->rollback();
            }
            if ($first === false) {
                M()->rollback();
            }
            return true;
        } else {
            $first = $this->where('uid='.$arr_id['first'])->setInc('integral',C('integral_config_first'));
            $data = array('uid'=>$arr_id['first'],'type'=>'4','integral'=>'+'.C('integral_config_first'),'changetime'=>time(),'changeuid'=>$_SESSION['onethink_admin']['user_auth']['uid']);
            $integral = M('integralchange');
            $res = $integral->data($data)->add();
            if ($res === false) {
                M()->rollback();
            }
            if ($first === false) {
                M()->rollback();
            }
            if (!array_key_exists('second', $arr_id)) {
                return true;
            }
            $second = $this->where('uid='.$arr_id['second'])->setInc('integral',C('integral_config_second'));
            $data = array('uid'=>$arr_id['second'],'type'=>'4','integral'=>'+'.C('integral_config_second'),'changetime'=>time(),'changeuid'=>$_SESSION['onethink_admin']['user_auth']['uid']);
            $integral = M('integralchange');
            $res = $integral->data($data)->add();
            if ($res === false) {
                M()->rollback();
            }
            if ($second === false) {
                M()->rollback();
            }
            if (!array_key_exists('third', $arr_id)) {
                return true;
            }
            $third = $this->where('uid='.$arr_id['third'])->setInc('integral',C('integral_config_third'));
            $data = array('uid'=>$arr_id['third'],'type'=>'4','integral'=>'+'.C('integral_config_third'),'changetime'=>time(),'changeuid'=>$_SESSION['onethink_admin']['user_auth']['uid']);
            $integral = M('integralchange');
            $res = $integral->data($data)->add();
            if ($res === false) {
                M()->rollback();
            }
            if ($third === false) {
                M()->rollback();
            }
            M()->commit();
            return true;
        }
    }

    /**
     * 加分策略
     * @param加分人id,加分数
     */
    public function add_integral($id,$integral)
    {
        $this->where(array('id'=>$id))->setInc('integral',$integral);

    }
}